﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:PeerReviewResult
    /// </summary>
    public partial class PeerReviewResult
    {
        public PeerReviewResult()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewResult GetModel(decimal FPeerReviewResultID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPeerReviewResultID,FPeerReviewID,FPeerReviewRuleCd,FEntityNo,FBeenVoterEmplNo,FBeenVoterClsCd,FVoterDeptNo,FBeenVoterDeptNo,FVoterEmplNo,FPeerReviewRuleScoreCd from dbo.t_PeerReviewResult ");
            strSql.Append(" where FPeerReviewResultID=@FPeerReviewResultID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPeerReviewResultID", SqlDbType.Decimal)
			};
            parameters[0].Value = FPeerReviewResultID;

            ElingOA6_Admin_DSJCJ.Model.PeerReviewResult model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewResult();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewResult DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.PeerReviewResult model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewResult();
            if (row != null)
            {
                if (row["FPeerReviewResultID"] != null && row["FPeerReviewResultID"].ToString() != "")
                {
                    model.FPeerReviewResultID = decimal.Parse(row["FPeerReviewResultID"].ToString());
                }
                if (row["FPeerReviewID"] != null && row["FPeerReviewID"].ToString() != "")
                {
                    model.FPeerReviewID = decimal.Parse(row["FPeerReviewID"].ToString());
                }
                if (row["FPeerReviewRuleCd"] != null)
                {
                    model.FPeerReviewRuleCd = row["FPeerReviewRuleCd"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FBeenVoterEmplNo"] != null)
                {
                    model.FBeenVoterEmplNo = row["FBeenVoterEmplNo"].ToString();
                }
                if (row["FBeenVoterClsCd"] != null)
                {
                    model.FBeenVoterClsCd = row["FBeenVoterClsCd"].ToString();
                }
                if (row["FVoterDeptNo"] != null)
                {
                    model.FVoterDeptNo = row["FVoterDeptNo"].ToString();
                }
                if (row["FBeenVoterDeptNo"] != null)
                {
                    model.FBeenVoterDeptNo = row["FBeenVoterDeptNo"].ToString();
                }
                if (row["FVoterEmplNo"] != null)
                {
                    model.FVoterEmplNo = row["FVoterEmplNo"].ToString();
                }
                if (row["FPeerReviewRuleScoreCd"] != null)
                {
                    model.FPeerReviewRuleScoreCd = row["FPeerReviewRuleScoreCd"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FPeerReviewResultID,FPeerReviewID,FPeerReviewRuleCd,FEntityNo,FBeenVoterEmplNo,FBeenVoterClsCd,FVoterDeptNo,FBeenVoterDeptNo,FVoterEmplNo,FPeerReviewRuleScoreCd ");
            strSql.Append(" FROM  dbo.t_PeerReviewResult ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetQueryList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  SELECT a.*,b.FScore,(b.FScore*a.sumcount) AS sumfen FROM (
SELECT  FBeenVoterEmplNo,FPeerReviewRuleCd,FPeerReviewRuleScoreCd , COUNT(*) AS sumcount FROM  dbo.t_PeerReviewResult
GROUP BY  FBeenVoterEmplNo,FPeerReviewRuleCd,FPeerReviewRuleScoreCd) AS a
LEFT JOIN dbo.t_PeerReviewRuleScore AS b ON a.FPeerReviewRuleScoreCd = b.FPeerReviewRuleScoreCd AND  a.FPeerReviewRuleCd = b.FPeerReviewRuleCd 
");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

    

        /// <summary>
        /// 增加方法
        /// </summary>
        /// <param name="peerReviewResultList">需增加的列表</param>
        /// <param name="Vmodel">更新其为已参加</param>
        public void Add(List<Model.PeerReviewResult> peerReviewResultList, Model.PeerReviewVoter Vmodel)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PeerReviewResult ");
            strSql.Append(" where FPeerReviewID=@FPeerReviewID and FVoterDeptNo=@FVoterDeptNo and FVoterEmplNo=@FVoterEmplNo ");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15)			};
            parameters3[0].Value = Vmodel.FPeerReviewID;
            parameters3[1].Value = Vmodel.FVoterDeptNo;
            parameters3[2].Value = Vmodel.FVoterEmplNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);


            foreach (Model.PeerReviewResult model in peerReviewResultList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into  dbo.t_PeerReviewResult(");
                strSql.Append("FPeerReviewID,FPeerReviewRuleCd,FEntityNo,FBeenVoterEmplNo,FBeenVoterClsCd,FVoterDeptNo,FBeenVoterDeptNo,FVoterEmplNo,FPeerReviewRuleScoreCd)");
                strSql.Append(" values (");
                strSql.Append("@FPeerReviewID,@FPeerReviewRuleCd,@FEntityNo,@FBeenVoterEmplNo,@FBeenVoterClsCd,@FVoterDeptNo,@FBeenVoterDeptNo,@FVoterEmplNo,@FPeerReviewRuleScoreCd)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FPeerReviewRuleCd", SqlDbType.NVarChar,20),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBeenVoterEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FBeenVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FPeerReviewRuleScoreCd", SqlDbType.NVarChar,20)};
                parameters[0].Value = model.FPeerReviewID;
                parameters[1].Value = model.FPeerReviewRuleCd;
                parameters[2].Value = model.FEntityNo;
                parameters[3].Value = model.FBeenVoterEmplNo;
                parameters[4].Value = model.FBeenVoterClsCd;
                parameters[5].Value = model.FVoterDeptNo;
                parameters[6].Value = model.FBeenVoterDeptNo;
                parameters[7].Value = model.FVoterEmplNo;
                parameters[8].Value = model.FPeerReviewRuleScoreCd;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters;
                cmdList.Add(cmds);
            }


            cmds = new CommandInfo();

            strSql = new StringBuilder();
            strSql.Append("update  dbo.t_PeerReviewVoter set ");
            strSql.Append("FStatus=@FStatus,");
            strSql.Append("FVoteDate=@FVoteDate,");
            strSql.Append("FVoteIP=@FVoteIP");
            strSql.Append(" where FPeerReviewID=@FPeerReviewID and FEntityNo=@FEntityNo and FVoterDeptNo=@FVoterDeptNo and FVoterEmplNo=@FVoterEmplNo ");
            SqlParameter[] parameters2 = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FVoteDate", SqlDbType.DateTime),
					new SqlParameter("@FVoteIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15)};
            parameters2[0].Value = Vmodel.FStatus;
            parameters2[1].Value = Vmodel.FVoteDate;
            parameters2[2].Value = Vmodel.FVoteIP;
            parameters2[3].Value = Vmodel.FPeerReviewID;
            parameters2[4].Value = Vmodel.FEntityNo;
            parameters2[5].Value = Vmodel.FVoterDeptNo;
            parameters2[6].Value = Vmodel.FVoterEmplNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);


            DbHelperSQL.ExecuteSqlTranWithString(cmdList);
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

    